#include "TCP.h"


void *pthread_client_read(void * soc_cli);
void *pthread_client_write(void * soc_cli);

int main(int argc, char const *argv[])
{
    if (argc != 3)
    {
       printf("格式错误：【%s】 端口 ip地址",argv[0]);
       return -1;
    }
    int soc_cil = client_init(atoi(argv[1]) ,argv[2] );
    //收发信息
    printf("开启聊天\n");
    pthread_t Tid[2];
    pthread_create(&Tid[0] , NULL , pthread_client_read , (void *)&soc_cil );
    pthread_create(&Tid[1] , NULL , pthread_client_write , (void *)&soc_cil );

    pthread_join(Tid[0],NULL);
    pthread_join(Tid[1],NULL);

    close_se_cl(NULL, &soc_cil);
    return 0;
}

void *pthread_client_read(void * soc_cli)
{
    int soc = *(int*)soc_cli;
    char buf[128]; 
    while (1)
    {
              
        memset(buf, 0, sizeof(buf));
        read(soc, buf, sizeof(buf));
        if (strlen(buf)==0)
        {
            continue;
        }
        
        if (!strcmp(buf, "quit"))
        {
            printf("退出成功\n");
            break;
        }
        printf("\n服务器：%s\n", buf);
    }
    return NULL;
}
void *pthread_client_write(void * soc_cli)
{
    int soc = *(int*)soc_cli;
    char buf[128];
    while (1)
    {
               // 发送信息
        memset(buf, 0, sizeof(buf));

        printf("客户端：");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf) - 1] = '\0';
        write(soc, buf, strlen(buf));
        if (!strcmp(buf, "quit\n"))
        {
            printf("退出成功\n");
            break;
        }
        // printf("发送的信息是[%s]\n", buf);
    }
    return  NULL;
}
